import { observable, action } from 'mobx';
import { Toast,Bubble,Tools } from 'weaver-mobile';
import { ListViewPage,RouteLayout } from 'weaver-mobile-page';
import * as Apis from "../../api/customerApis";
import * as adapter from '../../constants/adapter';
const getLabel = Tools.getLabel;
export default class WorkplanListStore {
  comObj = {};
  params = {};
  @observable showCheck = false;
  @observable list = {};
  @observable canEdit = false;
  @observable wpCreateLayout = {};
  @observable wpViewLayout = {};
  @observable initResponsed = false;// 是否已经得到初始化数据的响应
  @observable initSuccessed = false;// 初始化数据是否获取成功
  @observable initFailedMsg = "";   // 初始化处理失败的原因

  @action
  doNew = (comObj,params) =>{
    this.comObj = comObj;
    this.params = params;
  }

  @action
  doInit = () =>{
    let params = this.params;
    this.doInitCallBack(params);
  }

  @action
  doInitCallBack = (params,data) =>{
    this.initResponsed = true;
    this.initSuccessed = true;
    const currentPath = this.comObj.props.location.pathname;
    this.list = new ListViewPage.Store({ route:currentPath,id:"workplanList" });
    this.wpCreateLayout = new RouteLayout.Store({route:currentPath , id:'wpCreateLayout'});
    this.wpViewLayout = new RouteLayout.Store({route:currentPath , id:'wpViewLayout'});
    this.doSearch();
  }

  @action
  doSearch = () =>{
    let params = this.params;
    Apis.getWorkplanListResult(params).then(data => { 
      const dataKey = data.datas;
      this.list.clearData();
      this.list.getData({ dataKey });
    });
  }

  @action
  doClear = () =>{
    this.initResponsed = false;
    this.initSuccessed = false;
    this.initFailedMsg = "";
    this.list.clearData();
  }

  @action
  deleteWorkPlan = (id) =>{
    Toast.loading(getLabel('388626', '正在保存，请稍后...'), 0);
    Apis.deleteWorkPlan({workid:id}).then(data => {   
      if(this.comObj.props.saveCallBack){
        this.comObj.props.saveCallBack();
      }
      this.doSearch();
      Bubble.success(getLabel('83551',"保存成功"), 1, {backgroundColor:adapter.BUBBLE_BGCOLOR}, 2,()=>{        
        Toast.hide();
      });
    });
  }

  @action
  completeWorkPlan = (id) =>{
    Toast.loading(getLabel('388626', '正在保存，请稍后...'), 0);
    Apis.completeWorkPlan({planIds:id}).then(data => { 
      if(this.comObj.props.saveCallBack){
        this.comObj.props.saveCallBack();
      }
      this.doSearch();
      Bubble.success(getLabel('83551',"保存成功"), 1, {backgroundColor:adapter.BUBBLE_BGCOLOR}, 2,()=>{
        Toast.hide();
      });      
    });
  }
}